<?php
/**
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/13
 * Time: 9:04
 */
namespace app\home\controller;

use app\common\model\AccountCate as AccountCateModel;

/**
 * 记账类目控制器
 * Class Accountcate
 * @package app\home\controller
 */
class Accountcate extends Base
{
    /**
     * [index 获取类目]
     * @param
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new AccountCateModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            $map['comid'] = $param['comid'];
            $map['status'] = 1;
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map,$this->from, $this->size,'id,name,image');
            if($param['page'] == 1){
                $moren = $model->getAllData(['comid'=>0,'status'=>1],'id','id,name,image','asc');
                if(empty(count($lists))){
                    $lists = $moren;
                }else{
                    $lists = array_merge($moren,$lists);
                }
            }
        }catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountcate/index');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add 添加类目]
     * @param
     * @return json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new AccountCateModel;
        try {
            $param['image'] = 'https://fushan.oss-cn-hangzhou.aliyuncs.com/%E6%99%BA%E6%85%A7%E5%AF%BA%E9%99%A2/%E8%AE%B0%E8%B4%A6%E7%B1%BB%E7%9B%AE/%E6%B7%BB%E5%8A%A0%E7%B1%BB.png';
            $model->addData($param);
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,'home/Accountcate/add');
            return admin_json(-1, 'error' . $e->getMessage());
        }
        return admin_json(1, 'success');
    }
}